package com.dkm.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dkm.entity.Module;
import com.dkm.entity.Role;
import com.dkm.service.RoleService;
import com.dkm.service.TreeService;
import com.dkm.util.TreeNode;

@RestController
@RequestMapping("/index")
public class IndexController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private TreeService treeService;
	
	//根据用户的角色id获取用户的功能菜单
	@GetMapping("/getFunctionMenu")
	public List<TreeNode> getFunctionMenu(ServletRequest request) {
		//获取前台传的角色id数组
		String[] roleids=request.getParameterValues("roleids[]");
		//如果角色id为空
		if(roleids==null) {
			return null;
		}
		//将数组转换为list集合
		List<String> idList=new ArrayList<String>(Arrays.asList(roleids));
		//根据角色id集合批量获取角色的模块信息集合并去重
		List<Module> modules=roleService.findRolesByRoleIds(idList);
		//转换为树结构数据
		return treeService.getLayuiModuleTree(modules);
	}
}
